package com.example.shopping.me;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.shopping.R;

import java.util.ArrayList;
import java.util.List;

public class LoginActivity extends AppCompatActivity {
    TextView loginTv,regTv;
    ViewPager logVp;
    List<Fragment>fragList;
    @SuppressLint("ResourceAsColor")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        int pos=getIntent().getIntExtra("login",0);
        loginTv=findViewById(R.id.login_tv_log);
        regTv=findViewById(R.id.login_tv_reg);
        logVp=findViewById(R.id.login_vp);
        initPage();
        setVPListener();
        if (pos==0){
            loginTv.setTextColor(Color.rgb(200,10,200));
            regTv.setTextColor(Color.WHITE);
            logVp.setCurrentItem(0);
        }else {
            regTv.setTextColor(Color.rgb(200,10,200));
            loginTv.setTextColor(Color.WHITE);
            logVp.setCurrentItem(1);
        }

    }

    private void setVPListener() {
        logVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @SuppressLint("ResourceAsColor")
            @Override
            public void onPageSelected(int position) {
                if(position==0){
                    loginTv.setTextColor(Color.rgb(200,10,200));
                    regTv.setTextColor(Color.WHITE);
                }else {
                    regTv.setTextColor(Color.rgb(200,10,200));
                    loginTv.setTextColor(Color.WHITE);
                }
            }
        });
    }

    private void initPage() {
        fragList=new ArrayList<>();
        fragList.add(new LoginFragment());
        fragList.add(new RegFragment());
        LoginVpAdapter vpAdapter= new LoginVpAdapter(getSupportFragmentManager(),fragList);
        logVp.setAdapter(vpAdapter);

    }

    @SuppressLint("ResourceAsColor")
    public void onClick(View view) {
        switch (view .getId()){
            case R.id.login_tv_log:
                loginTv.setTextColor(Color.rgb(200,10,200));
                regTv.setTextColor(Color.WHITE);
                logVp.setCurrentItem(0);
            break;
            case R.id.login_tv_reg:
                regTv.setTextColor(Color.rgb(200,10,200));
                loginTv.setTextColor(Color.WHITE);
                logVp.setCurrentItem(1);
            break;
        }
    }
    //改变页面信息 在注册页面调用这个方法
    public void changePage(){
        logVp.setCurrentItem(0);

    }
}
